﻿using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using DG.Tweening;

public class ResultManager : MonoBehaviour {

    public static ResultManager _instance;
    //获取成绩text
    public Text finalScoreText;
    //获取重新开始按钮
    public Button Btn_restart;
    //获取返回主菜单按钮
    public Button Btn_back;
    //获取暂停按钮
    public Button Btn_pause;
    //暂停/继续
    public Image BestKing;
    //获取蛇
    public Snake _snake;
    //暂停状态
    private bool isPause = false;
    //最高分数
    private int _bestScore;
    //玩家姓名
    private string _name = "未知";
    //是否新纪录
    private bool _isNew = false;
    public Image inputName;
    public InputField input;
    public Button inputBtn;

    void Awake()
    {
        _instance = this;
    }
	// Use this for initialization
    void Start()
    {
        if (PlayerPrefs.HasKey("bestScore"))
        {
            _bestScore = PlayerPrefs.GetInt("bestScore");
        }
        else
        {
            _bestScore = 0;
        }
        RealManager._instance.BestScore.text = _bestScore.ToString();
        this.gameObject.SetActive(false);
        //重新开始事件
        Btn_restart.onClick.AddListener(() => {
            this.CancelInvoke();
            _snake.gameObject.SetActive(true);
            _snake.SendMessage("Reset");
            RealManager._instance.Reset();
            AppleFactory._instance.SendMessage("Create");
            this.gameObject.SetActive(false);
        });
        //返回主菜单事件
        Btn_back.onClick.AddListener(() => {
            if (_isNew)
            {
                PlayerPrefs.SetString("name",name);
                PlayerPrefs.SetInt("newScore", _bestScore);
            }
            Application.LoadLevel("MainScene");
        });
        //暂停游戏事件
        Btn_pause.onClick.AddListener(() =>
        {
            this.gameObject.SetActive(!isPause);
            if (isPause)
            {
                Time.timeScale = RealManager._instance._level;
                Btn_pause.GetComponentInChildren<Text>().text = "暂停";
                isPause = false;
            }
            else
            {
                Time.timeScale = 0;
                Btn_pause.GetComponentInChildren<Text>().text = "继续";
                isPause = true;
            }
            
        });
        inputBtn.onClick.AddListener(() => {
            _name = input.text;
            inputName.gameObject.SetActive(false);
        });

        inputName.gameObject.SetActive(false);
        BestKing.gameObject.SetActive(false);
	}
	
	// Update is called once per frame
    void Update()
    {

    }

    void Blink()
    {
        _snake.gameObject.SetActive(_snake.gameObject.active ? false : true);
    }

    void CheckScore()
    {
        if (RealManager._instance.Score>_bestScore)
        {
            BestKing.gameObject.SetActive(true);
            BestKing.transform.localPosition = new Vector3(0f, 0f, 0f);
            BestKing.transform.DOLocalMove(new Vector3(330f, 180f, 0f), 5);
            BestKing.transform.localScale = Vector3.one;
            BestKing.transform.DOScale(0.5f, 5f);
            _isNew = true;
            _bestScore = RealManager._instance.Score;
            inputName.gameObject.SetActive(true);
            RealManager._instance.BestScore.text = _bestScore.ToString();
        }
        else
        {
            inputName.gameObject.SetActive(false);
            BestKing.gameObject.SetActive(false);
        }
    }

    public void ShowScore()
    {
        this.gameObject.SetActive(true);
        finalScoreText.text = RealManager._instance.Score.ToString();
    }

    internal void StartBlink()
    {
        InvokeRepeating("Blink", 0.5f, 0.5f);
    }
}
